<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function __construct()  
    {  
        parent::__construct();  
        $res = isLogin();
        if($res == '1')
        {
            $this -> error('还未登录',__APP__.'/Home/Login/index');
        }

        $set = setConfig();

    } 

    public function index(){

        $arr['admin_id'] = session('admin_id');
        if($arr['admin_id'] == '')
        {
            $this -> error('还未登录',__APP__.'/Home/Login/index');
        }
        
        $this->display();
    }

    /*平台信息*/
    public function terrace()
    {
        $configModer = M('config');
        $configRes   = $configModer->select();
        $arr['buy'] = $configRes[0]['buy'];
    	$arr['weight'] = $configRes[0]['weight'];
    	$arr['return'] = $configRes[0]['return'];
    	$arr['commision'] = $configRes[0]['commision'];
        $arr['proxy'] = $configRes[0]['proxy'];

    	$this->assign('arr' ,$arr);
    	$this->display();
    }

    /*积分充值*/
    public function buy()
    {
        $this->display();
    }
    /*积分充值*/
    public function buyTo()
    {
        $configModer = M('config');
        $configRes   = $configModer->select();
        $arr['buy'] = $configRes[0]['buy'];
        $arr['phone'] = I('post.phone');
        $arr['num'] = I('post.num');

        $rechargeModel = D('Recharge');
        $res      = $rechargeModel->buyTo($arr);

        if($res == 'success')
        {
			/*商家积分充值日志*/
            $shop['phone'] = I('post.phone');
            $shopModel = D('Shop');
            $haveShop = $shopModel->where($shop)->find();
            $userArr['admin_id'] = $_SESSION['admin_id'];
            $userArr['admin_name'] = $_SESSION['name'];
            $userArr['object_id'] = $haveShop['shop_id'];
            $userArr['phone'] = $haveShop['phone'];
            $userArr['points'] = I('post.num');
            adminLog('givePoints', $userArr);
            /*商家积分充值日志end*/
			
            $this->success('充值成功');
        }
        elseif($res == 'noRecharge')
        {
            $this->error('充值日志增加失败');
        }
        elseif($res == 'noPay')
        {
            $this->error('支付失败');
        }
        elseif($res == 'noShop')
        {
            $this->error('商店不存在！');
        }
    }

    /*新入驻店铺*/
    public function newShop()
    {
    	$shopModel = M('Shop');
    	$shop['isApply'] = 0;
		$count      = $shopModel->where($shop)->count();
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $shopModel->where($shop)->order('shop_id')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	    //var_dump($show);exit();
		$this->display(); // 输出模板

    }

    /*新入驻店铺审核通过*/
    public function via($id)
    {
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');

    	$res = $shopModel->via($arr);

    	//var_dump($res);exit();
    	if($res)
    	{
    		$this->success('审核成功');
    	}else
    	{
    		$this->error('审核失败！');
    	}
    }

    /*新入驻店铺审核不通过*/
    public function noVia($id)
    {
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');
    	$res = $shopModel->noVia($arr);
    	if($res)
    	{
    		$this->success('审核成功');
    	}else
    	{
    		$this->error('审核失败！');
    	}
    }

    /*入驻店铺信息*/
    public function shop()
    {

    	$shopModel = M('Shop');
    	$shop['isApply'] = 1;
		$shop['apply'] = 1;
		$count      = $shopModel->where($shop)->count();
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $shopModel->where($shop)->order('shop_id')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	    //var_dump($show);exit();
    	$this->display();
    }	

    /*提现申请*/
    public function apply()
    {
        $payModel = D('Admin/Pay');
        $res = $payModel->apply();
        //var_dump($res);exit();
        $this->assign('res',$res);
        $this->display(); 

    }
	

	 /*打印*/
    public function dayinApply()
    {
		$payModel = D('Admin/Pay');
		if($_GET['startTime'] && $_GET['endTime']){
			
			$ress = $payModel->applyTime($_GET['startTime'],$_GET['endTime']);
		}else{
			$ress = $payModel->apply();
		}
        //$ress = $payModel->apply();
		$res = array();
        foreach($ress as $k => $v){
			$new_arr = array();
			$new_arr['name'] = $v['name'];
			$new_arr['phone'] = $v['phone'];
			$new_arr['idcard'] = $v['idcard'];
			$new_arr['card'] = $v['card'];
			$new_arr['banktype'] = $v['banktype'];
			$new_arr['money'] = $v['money'];
			$new_arr['shijian'] = $v['shijian'];                    
			$new_arr['type'] = '会员';	
			array_push($res,$new_arr);
		}
		
        //$a = $this->excle($res);
        $a = $this->exportDo($res);
		die;
        //$b = substr($a, 1);
        //header('Location:' . $b);
        //$this->success('导出成功!', __APP__.'/Home/Index/applyPay');
    }


	/*批量审核*/
	public function shenheApply(){
		if($_POST['startTime'] && $_POST['endTime']){
			$pay = M("pay");
			$startTime = $_POST['startTime'];
			$endTime = $_POST['endTime'];
			$arr = "apply =0 AND isApply =0  AND shijian >= '$startTime' AND shijian < '$endTime'";
			$list = $pay -> where($arr) -> order("shijian desc") -> select();
			foreach($list as $k => $v){
				$pay -> where(array('id'=>$v['id'])) -> setField('apply',1);
				$pay -> where(array('id'=>$v['id'])) -> setField('isApply',1);
			}
			$this -> ajaxReturn(1);
		}else{
			$this -> ajaxReturn(0);
		}
		
	}



	
    /*提现申请审核通过*/
    public function applyVia($id)
    {
        $arr['id'] = $id;
        $payModel = D('Admin/Pay');

        $res = $payModel->via($arr);

        if($res)
        {
            $this->success('审核成功');
        }else
        {
            $this->error('审核失败！');
        }
    }

    /*提现申请审核不通过*/
    public function applyNoVia($id)
    {
        $arr['id'] = $id;
        $payModel = D('Admin/Pay');
        $res = $payModel->noVia($arr);
        if($res)
        {
            $this->success('审核成功');
        }else
        {
            $this->error('审核失败！');
        }
    }

    /*店铺详情*/
    public function more($id)
    {
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');
    	$res = $shopModel->getShop($arr, 'shop');
    	
		$this->assign('res',$res);
    	$this->display();
    }

    /*更改店铺*/
    public function set($id)
    {
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');
    	$res = $shopModel->getShop($arr, 'shop');
    	
		$this->assign('res',$res);
    	$this->display();
    }
    /*更改店铺*/
    public function setTo()
    {
    	$arr['pid']   = trim(I('post.pid'));
        $arr['is_user']   = trim(I('post.is_user'));
    	$arr['shop_id']   = trim(I('post.id'));
        $arr['name']   = trim(I('post.name'));
        $arr['contacts']   = trim(I('post.contacts'));
        $arr['area'] = trim(I('post.area'));
        $arr['type']   = trim(I('post.type'));
        $arr['address'] = trim(I('post.address'));
        $arr['synopsis']   = trim(I('post.synopsis'));
        //var_dump($arr);exit;
		
		if($arr['is_user']==0){
			$secModel=M('shop');
			$res = $secModel->where(array('shop_id'=>$arr['pid']))->select();
		}else{
			$secModel=M('user');
			$res = $secModel->where(array('user_Id'=>$arr['pid']))->select();	
		}
		if(count($res) == 0){
			$this->error('填写的上级不存在！');
		}
		
        $shopModel = D('shop');
        $res = $shopModel->changed($arr);

        if($res) 
        {
        	$this->success('信息修改成功');
        }else
        {
        	$this -> error('信息修改失败');
        }
    }
    
    /*获取上下级*/
    public function level()
    {
    	//获取下级
    	$arr['shop_id'] = I('post.id');

    	$shopModel = D('shop');
        $res = $shopModel->myfans($arr['shop_id'], 'shop');
        $userRes = $shopModel->myfans($arr['shop_id'], 'user');

        //获取店铺的上级
        $shopRes = $shopModel->level($arr['shop_id']);
    	//var_dump($shopRes);exit();
    	$len = count($res);

        $this->assign('shopRes',$shopRes);
        $this->assign('len',$len);
		$this->assign('res',$res);
		$this->assign('userRes',$userRes);
    	$this->display();
    }


    /*删除店铺*/
    public function del($id)
    {
		/*删除商家日志*/
        $shop['id'] = $id;
        $shopModel = D('Shop');
        $haveShop = $shopModel->where($shop)->find();
        $userArr['admin_id'] = $_SESSION['admin_id'];
        $userArr['admin_name'] = $_SESSION['name'];
        $userArr['object_id'] = $id;
        $userArr['phone'] = $haveShop['phone'];
        adminLog('shopDel', $userArr);
        /*删除商家日志end*/
		
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');
    	$res = $shopModel->del($arr);
    	if($res)
    	{
    		$this->success('删除成功');
    	}else
    	{
    		$this->error('删除失败！');
    	}
    }

    /*会员管理*/
    public function user()
    {		
    	$userModel = M('user');
		$count      = $userModel->count();
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $userModel->order('user_Id')->limit($Page->firstRow.','.$Page->listRows)->select();
		//var_dump($list);exit;
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	    //var_dump($show);exit();
		$this->display(); // 输出模板
    }

	public function getUserList(){
		$user = M('user');
		$list = $user -> field('name,phone,idcard,card,openbank,banktype') -> select();
		$this -> exportUser($list);
		exit();
	}
	/*打印会员信息*/
    public function exportUser($data) {
		//header("Content-type: text/html; charset=utf-8");
        set_time_limit(0);

        $head = array('姓名','联系方式','身份证','卡号', '银行卡预留电话', '银行类型');



        array_unshift($data, $head);  //添加表头
        $file_name = date("Y-m-d",time()).'会员信息表';         //excel文件名

        //将用户信息组装成表格导出到excel中
        $str = ''; //初始化
        //输出表头
        $str .= '<table border="1">
                    <tr>
                        <td colspan="'.count($data[0]).'" align="center" style="font-size:16px; height:32px; font-weight:bold">'.$file_name.'</td>
                    </tr>';
        //循环输出行     
        foreach($data as $v1) {
            $str .= '<tr>';
            //循环输出列
            foreach($v1 as $v2) {
                $str .= '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>';
            }
            $str .= '</tr>';
        }
        $str .=    '</table>';

        //转换为GBK编码
        //$str = iconv("UTF-8","GBK",$str);

        //输出
					
 		ob_end_clean();
        header('Content-Length: '.strlen($str));
        header("Content-Type: application/vnd.ms-excel;charset=UTF-8");
        header("Content-Disposition:filename={$file_name}.xls");
		header('Cache-Control: max-age=0');
        echo $str;
    }	
	
	
	
	public function repassword(){
		$id = trim(I('id'));
		$user = M('user');
		$arr = $user -> where(array('user_Id'=>$id)) -> setField('pwd',"96e79218965eb72c92a549dd5a330112");
		if($arr){
			$this->success('密码重置成功');
		}else{
			$this -> error('重置密码和原密码相同');
		}
	}
	
    /*会员搜索*/
    public function sousuo()
    {
        $arr['phone'] = trim(I('phone'));

        $userModel = M('user');
        $res = $userModel->where($arr)->select();
        //var_dump($res);exit();
        $this->assign('res' ,$res);
        $this->display();
    }
	
	/*商家搜索*/
    public function sousuo1()
    {
        $arr['phone'] = trim(I('phone'));
    	$arr['isApply'] = 1;
		$arr['apply'] = 1;

        $shopModel = M('shop');
        $res = $shopModel->where($arr)->select();
        //var_dump($res);exit();
        $this->assign('res' ,$res);
        $this->display();
    }
	public function reshoppassword(){
		$id = trim(I('id'));
		$shop= M('shop');
		$arr = $shop-> where(array('shop_id'=>$id)) -> setField('pwd',"96e79218965eb72c92a549dd5a330112");
		if($arr){
			$this->success('密码重置成功');
		}else{
			$this -> error('重置密码和原密码相同');
		}
	}
	/*修改银行卡信息*/
	public function changeinfo(){
		$user = M('user');
		$info = $user -> where(array('user_Id'=>$_GET['id'])) -> find();
		$this -> assign('info',$info);
		$this -> display();
	}

	public function changepost(){
		$user = M('user');
		$user_id = trim(I('post.user_id'));
		$arr['name'] = trim(I('post.name'));
		$arr['card']= trim(I('post.card'));
		$arr['openbank'] = trim(I('post.openbank'));
		$arr['idcard'] = trim(I('post.idcard'));
		$ar = $user -> where(array('user_Id'=>$user_id)) ->save($arr);
		if($ar){
			$this->success('信息修改成功');
		}else{
			$this -> error('信息修改失败');
		}
	}
	
    /*更改*/
    public function uSet($id)
    {
        $arr['user_id'] = $id;
        $userModel = D('User');
        $res = $userModel->getUser($arr);
        
        $this->assign('res',$res);
        $this->display();
    }
    /*修改会员*/
    public function uSetTo()
    {
        $arr['user_id']   = trim(I('post.id'));
        $arr['pid']   = trim(I('post.pid'));
        $arr['is_user']   = trim(I('post.is_user'));
		
		if($arr['is_user']==0){
			$secModel=M('shop');
			$res = $secModel->where(array('shop_id'=>$arr['pid']))->select();
		}else{
			$secModel=M('user');
			$res = $secModel->where(array('user_Id'=>$arr['pid']))->select();	
		}
		if(count($res) == 0){
			$this->error('填写的上级不存在！');
		}
		
        $arr['pay_points']   = trim(I('post.pay_points'));
        $arr['return_points']   = trim(I('post.return_points'));
        $arr['use_points'] = trim(I('post.use_points'));
        
        $userModel = D('User');
        $res = $userModel->changed($arr);

        if($res) 
        {
            $this->success('信息修改成功');
        }else
        {
            $this -> error('信息修改失败');
        }
    }

    /*会员详情*/
    public function uMore($id)
    {
        $arr['user_id'] = $id;
        $userModel = D('User');
        $res = $userModel->getUser($arr);
        
        $this->assign('res',$res);
        $this->display();
    }

    /*会员删除*/
    public function uDel($id)
    {
		/*删除会员日志*/
        $user['id'] = $id;
        $userModel = D('User');
        $haveUser = $userModel->where($user)->find();
        $userArr['admin_id'] = $_SESSION['admin_id'];
        $userArr['admin_name'] = $_SESSION['name'];
        $userArr['object_id'] = $id;
        $userArr['phone'] = $haveUser['phone'];
        adminLog('userDel', $userArr);
        /*删除会员日志end*/
		
        $arr['user_id'] = $id;
        $userModel = D('User');
        $res = $userModel->del($arr);

        if($res)
        {
            $this->success('删除成功');
        }else
        {
            $this->error('删除失败！');
        }
    }

    /*获取上下级*/
    public function uLevel()
    {
        //获取下级
        $arr['user_Id'] = I('post.id');

        $userModel = D( 'Admin/User' );
        $res = $userModel->myfans($arr['user_Id'], 'shop');
        $userRes = $userModel->myfans($arr['user_Id'], 'user');

        $levelRes = $userModel->level($arr['user_Id']);
        $len = count($res);
        //var_dump($userRes);echo'-----------------';var_dump($levelRes);exit();

        $this->assign('levelRes',$levelRes);
        $this->assign('len',$len);
        $this->assign('res',$res);
        $this->assign('userRes',$userRes);
        $this->display();
    }

    /*地区列表*/
    public function place()
    {
        $userModel = D( 'Seller/Area' );
        $res = $userModel->index('area');
        //var_dump($res);exit;
        $this->assign('res',$res);
        $this->display();
    }

    /*修改地区*/
    public function areaSet($id)
    {
        //var_dump($area);exit;
        $userModel = D( 'Seller/Area' );
        $res = $userModel->one($id, 'area');
        $this->assign('res',$res);
        $this->display();
    }

    /*修改地区*/
    public function areasetTo()
    {
        $area['area_id'] = I('post.id');
        $area['name'] = trim(I('post.name'));

        $areaModel = D( 'Seller/Area' );
        $res = $areaModel->set($area, 'area');
        if($res)
        {
            $this->success('修改成功');
        }else
        {
            $this->error('修改失败！');
        }
    }
    /*地区删除*/
    public function aDel($id)
    {
        $arr['area_id'] = $id;
        $areaModel = D( 'Seller/Area' );
        $res = $areaModel->del($arr, 'area');

        if($res)
        {
            $this->success('删除成功');
        }else
        {
            $this->error('删除失败！');
        }
    }

    /*增加*/
    public function areaAdd()
    {
        $area['name'] = I('post.name');
        $areaModel = D( 'Seller/Area' );
        $res = $areaModel->areaAdd($area, 'area');

        if($res)
        {
            $this->success('增加成功');
        }else
        {
            $this->error('增加失败！');
        }
    }

    /*商品分类列表*/
    public function cla()
    {
        $userModel = D( 'Seller/Class' );
        $res = $userModel->index('class');
        //var_dump($res);exit;
        $this->assign('res',$res);
        $this->display();
    }

    /*修改商品分类*/
    public function classSet($id)
    {
        $userModel = D( 'Seller/Class' );
        $res = $userModel->one($id, 'class');
        
        $this->assign('res',$res);
        $this->display();
    }

    /*修改商品分类*/
    public function classsetTo()
    {
        $class['id'] = I('post.id');
        $class['name'] = trim(I('post.name'));
        //var_dump($class);exit;
        $areaModel = D( 'Seller/Class' );
        $res = $areaModel->set($class, 'class');
        if($res == 'notIn')
        {
            $this->error('修改失败！');
        }else
        {
            $this->success('修改成功');
        }
    }
    /*商品分类删除*/
    public function cDel($id)
    {
        $arr['class_id'] = $id;
        $areaModel = D( 'Seller/Class' );
        $res = $areaModel->del($arr, 'class');

        if($res)
        {
            $this->success('删除成功');
        }else
        {
            $this->error('删除失败！');
        }
    }

    /*增加商品分类*/
    public function classAdd()
    {
        $class['name'] = I('post.name');

        $areaModel = D( 'Seller/Class' );
        $res = $areaModel->areaAdd($class, 'class');

        if($res == 'notIn')
        {
            $this->error('增加失败！');
        }else
        {
            $this->success('增加成功');
        }
    }

    /*修改密码*/
    public function pwd()
    {
        $arr['admin_id'] = session('admin_id');

        $adminModel = M( 'admin' );
        $res = $adminModel->where($arr)->select();

        $this->assign('res', $res);
        $this->display();
    }

    /*修改密码*/
    public function pwdTo()
    {
        $old['pwd'] = md5(trim(I('post.mpass')));
        $arr['newpass'] = md5(trim(I('post.newpass')));
        $new['pwd'] = md5(trim(I('post.renewpass')));

        
        $adminModel = M('admin');
        $adminRes = $adminModel->where($old)->select();
        if($adminRes)
        {
            $adminId['admin_id'] = $adminRes[0]['admin_id'];
            //var_dump($adminId);exit();
            $save = $adminModel->data($new)->where($adminId)->save();
            if($save)
            {
                $this->success('修改成功');
            }else
            {
                $this->error('修改失败');//密码修改失败
            }
        }else
        {
            $this->error('旧密码错误');//旧密码错误
        }

    }

	public function applypay(){
		$pay = M("pay");
		$user = M("user");
		$shop = M("shop");
		$count = $pay -> where("apply =1 AND isApply = 1 AND money < 0") -> count();
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $pay->order('shijian desc,id desc')->where("apply =1 AND isApply = 1 AND money < 0")->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $k => $v){
			if($v['is_user'] == 1){
				$de_info = $user -> where(array('user_Id'=>$v['user_id'])) -> find();
				$list[$k]['type'] = '会员';
			}else{
				$de_info = $shop -> where(array('shop_id'=>$v['shop_id'])) -> find();
				$list[$k]['type'] = '商家';	
			}

			$list[$k]['phone'] = $de_info['phone'];
			$list[$k]['idcard'] = $de_info['idcard'];
			$list[$k]['card'] = $de_info['card'];
			$list[$k]['banktype'] = $de_info['banktype'];
			$list[$k]['name'] = $de_info['name'];
			
			if($v['apply'] == 0 && $v['isApply'] == 0){
				$list[$k]['statusname'] = "审核中";
			}else if($v['apply'] == 0 && $v['isApply'] == 1){
				$list[$k]['statusname'] = "审核不通过";
			}else if($v['apply'] == 1 && $v['isApply'] == 1){
				$list[$k]['statusname'] = "审核通过";
			}

			unset($de_info);
			
		}
		
        $this->assign("res",$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();		
		
	}
	

	/**打印**/
	public function dayinWithDraw(){
		$payModel = D('Admin/Pay');
		if($_GET['startTime'] && $_GET['endTime']){
			
			$ress = $payModel->applyYesTime($_GET['startTime'],$_GET['endTime']);
		}else{
			$ress = $payModel->applyYes();
		}
        //$ress = $payModel->apply();
		$res = array();
        foreach($ress as $k => $v){
			$new_arr = array();
			$new_arr['name'] = $v['name'];
			$new_arr['phone'] = $v['phone'];
			$new_arr['idcard'] = $v['idcard'];
			$new_arr['card'] = $v['card'];
			$new_arr['banktype'] = $v['banktype'];
			$new_arr['money'] = $v['money'];
			$new_arr['shijian'] = $v['shijian'];                    
			$new_arr['type'] = $v['type'];
			array_push($res,$new_arr);
		}
		
        //$a = $this->excle($res);
        $a = $this->exportDo($res);
		die;
	}
	
    /*提现记录*/
    public function applyPay001()
    {
        //获取商家提现
        $payModel = D('Admin/Pay');
        $res = $payModel->applyPay();
        
        import('ORG.Util.Page');// 导入分页类
        $count=count($res);//得到数组元素个数
        $Page= new \Think\Page($count,25);
        //$Page= new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $newarray_1 = array_slice($res,$Page->firstRow,$Page->listRows);
        $show= $Page->show();// 分页显示输出﻿
        
        $this->assign("res",$newarray_1);
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); 

    }

    /*打印*/
    public function dayin()
    {
        $payModel = D('Admin/Pay');
        $res = $payModel->applyPay();
        
        $a = $this->excle($res);
        $b = substr($a, 1);
        header('Location:' . $b);
        //$this->success('导出成功!', __APP__.'/Home/Index/applyPay');
    }

    /**
     * 用户信息导出为Excel(方法一)
     * 优点：输出的数据列格式可以自定义，如数值、字符串等
     * 缺点：需要将二维表数据组装成html表格代码，代码简洁较繁杂
     */
    public function exportDo($data) {
		//header("Content-type: text/html; charset=utf-8");
        set_time_limit(0);

        $head = array('姓名','联系方式','身份证','卡号', '银行卡预留电话', '申请金额', '申请时间', '申请人类型');



        array_unshift($data, $head);  //添加表头
        $file_name = date("Y-m-d",time()).'提现申请表';         //excel文件名

        //将用户信息组装成表格导出到excel中
        $str = ''; //初始化
        //输出表头
        $str .= '<table border="1">
                    <tr>
                        <td colspan="'.count($data[0]).'" align="center" style="font-size:16px; height:32px; font-weight:bold">'.$file_name.'</td>
                    </tr>';
        //循环输出行     
        foreach($data as $v1) {
            $str .= '<tr>';
            //循环输出列
            foreach($v1 as $v2) {
                $str .= '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>';
            }
            $str .= '</tr>';
        }
        $str .=    '</table>';

        //转换为GBK编码
        //$str = iconv("UTF-8","GBK",$str);

        //输出
					
 		ob_end_clean();
        header('Content-Length: '.strlen($str));
        header("Content-Type: application/vnd.ms-excel;charset=UTF-8");
        header("Content-Disposition:filename={$file_name}.xls");
		header('Cache-Control: max-age=0');
        echo $str;
    }	
	
	
	
	
	
	
	
	
	
     public function excle($order) {
        $table_head = array('姓名','联系方式','身份证','卡号', '开户行', '申请金额', '申请时间', '申请人类型');
        $table_name = './' . time() . '.xls';
        $table_file = fopen($table_name, 'w');

        foreach ($table_head as $str) {
            $table_heads.=iconv('utf-8', 'gb2312', $str) . chr(9);
        }
        fwrite($table_file, $table_heads);
        foreach ($order as &$rows) {
			//$rows['idcard'] = "s".$rows['idcard'];
            $table_line = chr(13);
            $table_line.=iconv('utf-8', 'gb2312', $rows['name']) . chr(9);
            $table_line.=' ' . (string)$rows['phone'] . chr(9);
            $table_line.=$rows['idcard']. ' ' . chr(9);
            $table_line.=' ' . (string)$rows['card'] . chr(9);
            $table_line.=iconv('utf-8', 'gb2312', $rows['banktype']) . chr(9);
            $table_line.=iconv('utf-8', 'gb2312', $rows['money']) . chr(9);
            $table_line.=$rows['shijian'] . chr(9);
            $table_line.=iconv('utf-8', 'gb2312', $rows['type']) . chr(9);
            
            fwrite($table_file, $table_line);
        }
        fclose($table_file);
        return $table_name;
    }

	
	/*打印充值记录*/
	public function dayinApplyChong(){
		$pay = M("pay");
		$shop = M("shop");		
		if($_GET['startTime'] && $_GET['endTime']){
			$startTime = $_GET['startTime'];
			$endTime = $_GET['endTime'];		
		}else{
			
		}
		$arr['apply'] = 1;
		$arr['isApply'] = 1;
		$arr['money'] = array('gt',0);		
		$arr['user_id'] = array('eq',"");
		$arr['is_user'] = array('eq',"");
		$arr = "apply =1 AND isApply =1 AND money>0 AND user_id = 0 AND is_user=0  AND shijian >= '$startTime' AND shijian < '$endTime'";	
		$list = $pay -> where($arr) -> select();				
		foreach($list as $k => $v){
			$shop_info = $shop -> where(array('shop_id'=>$v['shop_id'])) -> find();
			$list[$k]['contacts'] = $shop_info['contacts'];
			$list[$k]['phone'] = $shop_info['phone'];
			$list[$k]['type'] = '商家';
		}		
        $a = $this->excle1($list);
        $b = substr($a, 1);
        header('Location:' . $b);
		exit();		
	}	
	
    /*打印充值记录*/
    public function dayin1()
    {
        $payModel = D('Admin/Pay');
        $res = $payModel->pay();
        
        $a = $this->excle1($res);
        $b = substr($a, 1);
        header('Location:' . $b);
        //$this->success('导出成功!', __APP__.'/Home/Index/applyPay');
    }

     public function excle1($order) {
        $table_head = array('姓名','联系方式', '充值金额', '充值时间', '充值人类型');
        $table_name = './' . time() . '.xls';
        $table_file = fopen($table_name, 'w');

        foreach ($table_head as $str) {
            $table_heads.=iconv('utf-8', 'gb2312', $str) . chr(9);
        }
        fwrite($table_file, $table_heads);
        foreach ($order as &$rows) {
            $table_line = chr(13);
            $table_line.=iconv('utf-8', 'gb2312', $rows['contacts']) . chr(9);
            $table_line.=$rows['phone'] . chr(9);
            $table_line.=iconv('utf-8', 'gb2312', $rows['money']) . chr(9);
            $table_line.=$rows['shijian'] . chr(9);
            $table_line.=iconv('utf-8', 'gb2312', $rows['type']) . chr(9);
            
            fwrite($table_file, $table_line);
        }
        fclose($table_file);
        return $table_name;
    }


    /*充值记录*/
    public function pay()
    {
		
		$pay = M("pay");
		$shop = M("shop");
		$arr['apply'] = 1;
		$arr['isApply'] = 1;
		$arr['money'] = array('gt',0);		
		$arr['user_id'] = array('eq',"");
		$arr['is_user'] = array('eq',"");
		
		$count = $pay -> where($arr) -> count();
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $pay->order('shijian desc,id desc')->where($arr)->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $k => $v){
			$shop_info = $shop -> where(array('shop_id'=>$v['shop_id'])) -> find();
			$list[$k]['contacts'] = $shop_info['contacts'];
			$list[$k]['phone'] = $shop_info['phone'];
		}
        
        $this->assign("res",$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); 
    }

    /*平台信息修改*/
    public function setTerrace()
    {
        $configModer = M('config');
        $configRes   = $configModer->select();
        $arr['buy'] = $configRes[0]['buy'];
        $arr['weight'] = $configRes[0]['weight'];
        $arr['return'] = $configRes[0]['return'];
        $arr['commision'] = $configRes[0]['commision'];
        $arr['proxy'] = $configRes[0]['proxy'];

        $this->assign('arr' ,$arr);
        $this->display();
    }
    public function setTreeaceTo()
    {
        $arr['weight'] = trim(I('post.weight'));
        $arr['return'] = trim(I('post.return'));
        $arr['commision'] = trim(I('post.commision'));
        $arr['buy'] = trim(I('post.buy'));
        $arr['proxy'] = trim(I('post.proxy'));
        
        $config['Id'] = 1;
        $configModel = M('config');
        $save = $configModel->data($arr)->where($config)->save();
        
        if($save)
        {
            $this->success('修改成功');
        }else
        {
            $this->error('修改失败');//密码修改失败
        }


    }

    /*会员代理商*/
    public function areaProxy()
    {

        $userModel = M('user');
        $user['is_proxy'] = 1;
        $count      = $userModel->where($user)->count();
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $userModel->where($user)->order('user_Id')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        //var_dump($show);exit();
        $this->display();
    }   

    /*代理商：会员搜索*/
    public function find()
    {
        $arr['phone'] = trim(I('phone'));

        $userModel = M('user');
        $res = $userModel->where($arr)->select();
        
        $this->assign('res' ,$res);
        $this->display();
    }
    /*设置为代理商*/
    public function ufind($id)
    {
        $arr['user_Id'] = $id;

        $userModel = M('user');
        $res = $userModel->where($arr)->select();

        if($res[0]['is_proxy']){
            $this->error('当前用户已是代理商！');
        }else{
            $user['is_proxy'] = 1;
            $save = $userModel->data($user)->where($arr)->save();

            if($save){
                $this->success('设置代理商成功！');
            }else{
                $this->error('设置代理商失败！');
            }
        }
    }
	
		
    /*取消为会员代理商*/
    public function noProxy($id)
    {
        $arr['user_Id'] = $id;

        $userModel = M('user');
        $res = $userModel->where($arr)->select();

        if($res[0]['is_proxy']){
            
            $user['user_Id'] = $id;
            $user['is_proxy'] = 0;
            $save = $userModel->data($user)->where($arr)->save();

            if($save){
                $this->success('取消代理商成功！');
            }else{
                $this->error('取消代理商失败！');
            }
        }else{
            $this->error('当前用户不是代理商！');
        }
    }


	/*商家代理商*/
    public function shopProxy()
    {

        $shopModel = M('shop');
        $shop['is_proxy'] = 1;
        $shop['apply'] = 1;
        $shop['isApply'] = 1;
        $count      = $shopModel->where($shop)->count();
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $shopModel->where($shop)->order('shop_id')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        //var_dump($show);exit();
        $this->display();
    } 

   /*代理商：商家搜索*/
    public function findShop()
    {
        $arr['phone'] = trim(I('phone'));        
        $arr['apply'] = 1;
        $arr['isApply'] = 1;

        $shopModel = M('shop');
        $res = $shopModel->where($arr)->select();
        
        $this->assign('res' ,$res);
        $this->display();
    }
	
    /*设置为商家代理商*/
    public function sfind($id)
    {
        $arr['shop_id'] = $id;
        $arr['apply'] = 1;
        $arr['isApply'] = 1;

        $shopModel = M('shop');
        $res = $shopModel->where($arr)->select();

        if($res[0]['is_proxy']){
            $this->error('当前用户已是代理商！');
        }else{
            $shop['is_proxy'] = 1;
            $shop['apply'] = 1;
            $shop['isApply'] = 1;
            $save = $shopModel->data($shop)->where($arr)->save();

            if($save){
                $this->success('设置代理商成功！');
            }else{
                $this->error('设置代理商失败！');
            }
        }
    }
		
    /*取消为商家代理商*/
    public function noShopProxy($id)
    {
        $arr['shop_id'] = $id;

        $shopModel = M('shop');
        $res = $shopModel->where($arr)->select();

        if($res[0]['is_proxy']){
            $shop['shop_id'] = $id;
            $shop['apply'] = 1;
            $shop['isApply'] = 1;
            $shop['is_proxy'] = 0;
            $save = $shopModel->data($shop)->where($arr)->save();

            if($save){
                $this->success('取消代理商成功！');
            }else{
                $this->error('取消代理商失败！');
            }
            
        }else{
            $this->error('当前用户不是代理商！');
        }
    }

    /*会员代理商*/
    public function unit()
    {

        $unitModel = M('unit');       
        $count      = $unitModel->count();
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $unitModel->order('shijian desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        //计算当前所有积分
        $uModel = D('Unit');  
        $all = $uModel->number();

        $this->assign('all',$all);// 当前账户总积分
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        
        $this->display();
    }

    /*管理员*/
    public function setAdmin()
    {
        $adminModel = M('admin');
        $admin['power'] = 1;       
        $count      = $adminModel->where($admin)->count();
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $adminModel->where($admin)->order('admin_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    /*增加管理员*/
    public function adminAdd()
    {
        $admin['name'] = trim(I('name'));
        $admin['pwd'] = md5(I('post.pwd'));
        $admin['power'] = 1;

        $adminModel = M('admin');
        $adminRes = $adminModel->data($admin)->add();
        if($adminRes)
        {
            $this->success('管理员添加成功', 'setAdmin');
        }else
        {
            $this->error('管理员添加失败', 'setAdmin');
        }
    }

    /*修改管理员*/
    public function adminUpdate($id)
    {
        $adminModel = M( 'admin' );
        $admin['admin_id'] = $id;
        $res = $adminModel->where($admin)->select();
        $this->assign('res',$res);
        $this->display();
    }
    /*修改管理员*/
    public function adminSetTo()
    {
        $area['name'] = trim(I('post.name'));
        $area['pwd'] = md5(I('post.mpass'));

        $adminModel = M( 'admin' );
        $res = $adminModel->where($area)->select();
        if($res)
        {
            $min['admin_id'] = $res[0]['admin_id'];
            $admin['name'] = trim(I('post.name'));
            $admin['pwd'] = md5(I('post.renewpass'));
            $adminTo = $adminModel->data($admin)->where($min)->save();
            if($adminTo)
            {
                $this->success('管理员修改成功', 'setAdmin');
            }else
            {
                $this->error('管理员修改失败', 'setAdmin');
            }
        }else
        {
            $this->error('旧密码错误', 'setAdmin');
        }
    }
    /*管理员删除*/
    public function admindelete($id)
    {
        $adminModel = M( 'admin' );
        $admin['admin_id'] = $id;
        $res = $adminModel->where($admin)->delete();

        if($res)
        {
            $this->success('删除成功');
        }else
        {
            $this->error('删除失败！');
        }
    }
	
	
    /*商家记录*/
    public function shopNum()
    {
        $this->display();
    }
    /*商家记录搜索*/
    public function finds()
    {
        session('phone',trim(I('phone')));
        $arr['phone'] = session('phone');
        $shopModel = M('shop');
        $shopRes = $shopModel->where($arr)->find();

        $recharge['shop_id'] = $shopRes['shop_id'];
        $rechargeModel = M('recharge');
        $count      = $rechargeModel->where($recharge)->count();
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $rechargeModel->where($recharge)->order('shijian desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('phone',$arr['phone']);// 当前账户总积分
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    /*会员记录*/
    public function userNum()
    {
        $this->display();
    }
    /*会员记录搜索*/
    public function findu()
    {
        session('phone',trim(I('phone')));
        $arr['phone'] = session('phone');
        $userModel = M('user');
        $userRes = $userModel->where($arr)->find();

        $log['user_Id'] = $userRes['user_id'];
        $logModel = M('log');
        $count      = $logModel->where($log)->count();
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $logModel->where($log)->order('shijian desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('phone',$arr['phone']);// 当前账户总积分
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }
	
	/**/
    public function zsjf()
    {
        $this->display();
    }
	/**/
    public function zsjfTo()
    {
        $points['shop_id'] = trim(I('post.shop_id'));
        $points['phone']   = trim(I('post.phone'));
        $points['number']   = trim(I('post.number'));
        $points['reason']   = '消费积分';
		
		print_r($points);
        $shopModel = D('Shop');
        $res = $shopModel->givePoints($points);

        if($res == 'notPhone')
        {
        	$this -> error('填写的电话格式不正确！');
        }elseif($res == 'notHas')
        {
        	$this -> error('此会员不存在！');
        }elseif($res == 'rechargeErr')
        {
        	$this -> error('商家充值失败');
        }elseif($res == 'logErr')
        {
        	$this -> error('会员充值失败！');
        }elseif($res == 'logSession')
        {
        	$this -> success('会员充值成功！');
        }elseif($res == 'noNumber')
        {
            $this -> success('余额不足！');
        }
    }

	public function my_pay(){
		$pay = M("wxpay");
		$shop = M("shop");
		
		$count      = $pay->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $pay->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		foreach($list as $k => $v){
			$list[$k]['createtime'] = date("Y-m-d H:i:s",$v['createtime']);
			$list[$k]['phone'] = $shop -> where(array('shop_id'=> $v['shopid'])) -> getField('phone');
		}

		$this->assign('res',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出						
		$this -> display();
	}
	
	
    public function adminLogs()
    {
        $adminLogModel = M('admin_log');
        $count      = $adminLogModel->count();
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $adminLogModel->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

	public function todayLogs()
    {
        $adminLogModel = M('admin_log');
        $admin['title'] = '积分充值';
        $admin['year'] = date('Y', time());
        $admin['money'] = date('m', time());
        $admin['day'] = date('d', time());
        $count      = $adminLogModel->where($admin)->count();
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $adminLogModel->where($admin)->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        /*计算今日积分*/
        $res      = $adminLogModel->where($admin)->select();
        $num = 0;
        for ($i=0; $i < count($res); $i++) { 
            $num = $res[$i]['points'] + $num;
        }
        /*计算今日积分end*/
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('all',$num);
        $this->display();
    }

    //自定义积分赠送比
    public function integralReturn () {
        $model = M('integral');
        $list  = $model->order('year')->order('moth')->select();
        $this->assign('list',$list);
        $this->display();
    }
    //改变状态
    public function changeStateintegral () {
	    $id         =   I('post.id');
	    $type       =   I('post.type');
	    $model      =   M('integral');
        $li         =   $model->find($id);
        if($li) {
            if($type == 0) {
                $model->run_state = 1;
            }else{
                $model->run_state = 0;
            }
            $res = $model->where('id='.$id)->save();
            if($res){
                $this->ajaxReturn(["state"=>1,"msg"=>"修改成功"]);
            }
            $this->ajaxReturn(["state"=>0,"msg"=>"修改失败"]);
        }
        $this->ajaxReturn(["state"=>0,"msg"=>"错误的请求"]);
    }

    //设置自定义积分赠送比
    public function setIntergral() {

	    $id = I('get.id');
        $model = M('integral');
        $data = $model->find($id);
        $moth = [
            ["item"=>'01',"v"=>'01'],
            ["item"=>'02',"v"=>'02'],
            ["item"=>'03',"v"=>'03'],
            ["item"=>'04',"v"=>'04'],
            ["item"=>'05',"v"=>'05'],
            ["item"=>'06',"v"=>'06'],
            ["item"=>'07',"v"=>'07'],
            ["item"=>'08',"v"=>'08'],
            ["item"=>'09',"v"=>'09'],
            ["item"=>'10',"v"=>'10'],
            ["item"=>'11',"v"=>'11'],
            ["item"=>'12',"v"=>'12'],
        ];
        $this->assign('data',$data);
        $this->assign('id',$id);
        $this->assign('mothlist',$moth);
        $this->assign('moth',$data['moth']);
        $this->assign('datajson',json_encode($data));
        $this->display();
    }

    //处理添加自定义积分
    public function setHandleIntergral() {
        $query = I('get.');
        $model = M('integral');
        if($query['id']) {
            //修改

            $haserror = $model
                ->where('year='.$query['year'])
                ->where('moth='.$query['moth'])
                ->find();

            if($haserror && $query['id'] != $haserror['id']){
                $this->error('修改失败，月份重复');
            }

            if($query['run_state']) {
                $query['run_state'] =1;
            }else{
                $query['run_state'] =0;
            }
            $model->where('id='.$query['id'])->save($query);
            $this->success('修改成功!');
        }else{
            //新增
            $haserror = $model
                ->where('year='.$query['year'])
                ->where('moth='.$query['moth'])
                ->find();
            if($haserror){
                $this->error('在一年中存在相同月份');
            }

            unset($query['id']);
            if($query['run_state']) {
                $query['run_state'] =1;
            }else{
                $query['run_state'] =0;
            }
            $query['created_time'] = time();
            $model->data($query)->add();
            $this->success('添加成功');
        }

    }


    //删除自定义积分项
    public function delIntergral() {
        $id = I('get.id');
        $model = M('integral');
        if($model->delete($id)){
            $this->success('删除成功');
        }else{
            $this->success('删除失败');
        };
    }
	

}